/*
 * Message.cpp
 *
 *  Created on: 05/07/2013
 *      Author: gilvan
 */

#include "Message.h"

#include <cstdlib>

Message::Message()
	: command(), broadcast(true)  {

}

Message::Message( const String& cmd, bool broad )
	: command(cmd), broadcast(broad)
{

}

Message::~Message() {
}

const String& Message::getCommand(void) const
{
	return command;
}
bool Message::isBroadcast(void) const
{
	return broadcast;
}

void Message::fromString(const String& str)
{
	StringVector tokens = split(str);
	if( tokens.size() >= 2 )
	{
		command = tokens[0];
		broadcast = atoi( tokens[1].c_str() );

		_fromString( tokens );
	}
}

StringVector Message::split(const String& str)
{
	StringVector retorno;
	String messageElement;
	messageElement.reserve(16);
	for (unsigned int i = 0; i < str.size(); ++i)
	{
		if (str[i] == '|')
	{
			retorno.push_back(messageElement);
			messageElement.clear();
		}
		else
		{
			messageElement += str[i];
		}
	}
	retorno.push_back(messageElement);
	return retorno;
}
